package com.stone;

public class TreeNode {
    private Integer val;
    private TreeNode left;
    private TreeNode right;

    public TreeNode(int x) {
        this.val = x;
    }
    public TreeNode() {
    }
    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public Integer getVal() {
        return val;
    }

    public TreeNode setVal(int val) {
        this.val = val;
        return null;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "val=" + val +
                ", left=" + left +
                ", right=" + right +
                '}';
    }

    private void inOrderTransvalWithIterate(TreeNode node) {

    }

    private void inOrderTransvalWithRecursive(TreeNode node) {

        if (node == null) {
            System.out.println("二叉树为空，无法遍历");
        }

        //递归遍历左子树中序遍历
        if (node.left != null) {
            node.left.inOrderTransvalWithRecursive(node);
        }

        //输出父节点
        System.out.println(node.getVal());

        //递归遍历右子树中序遍历
        if (node.right != null) {
            node.left.inOrderTransvalWithRecursive(node);
        }
    }

    public static void main(String[] args) {
        final int[] values = { 1, 3, 4, 5, 2, 8, 6, 7, 9, 0 };
        // TreeNode binaryTreeByArray = createBinaryTreeByArray(values, 9);

        TreeNode root=new TreeNode(9);
        // for (int i = 0; i < values.length-1; i++) {
            bulitTree(root);
        // }

        System.out.println(root);


    }
    static TreeNode createBinaryTreeByArray(int[] values, int index){
        TreeNode tn = null;
        if (index<values.length) {
            int value = values[index];
            tn = new TreeNode(value);
            tn.left = createBinaryTreeByArray(values, 2*index+1);
            tn.right = createBinaryTreeByArray(values, 2*index+2);
            return tn;
        }
        return tn;
    }

    public static void bulitTree(TreeNode root) {
        final int[] values = {1, 3, 4, 5, 2, 8, 6, 7, 9, 0};
        for (int i = 0; i < values.length - 1; i++) {
            //新加入来的节点先与根节点的值进行比较，小于根节点放入左子树，大于根节点放入右子树
            if (values[i] < root.val) {
                if (root.left == null) {
                    root.left = new TreeNode();
                    root.left.val = values[i];
                } else
                    //左子树递归建立二叉树
                    root.left.bulitTree(root);
            } else if (values[i] > root.val) {
                if (root.right == null) {
                    root.right = new TreeNode();
                    root.right.val = values[i];
                } else
                    //右子树递归建立二叉树
                    root.right.bulitTree(root);
            }
        }
    }
}
